#!/usr/bin/env bash

EXECUTION_PATH="$PWD"
PISTON_PATH="$(dirname "$(realpath "$0")")"

cd "$PISTON_PATH"
PISTON_ENV=$(cat .piston_env 2> /dev/null || echo dev)

docker_compose(){
    if [ -f "docker-compose.$PISTON_ENV.yaml" ]; then
        docker-compose -f "docker-compose.$PISTON_ENV.yaml" "$@"
    else
        docker-compose "$@"
    fi
}

case $1 in
    help)
        echo "=== Piston Management ==="
        echo "Current Environment: $PISTON_ENV"
        echo
        echo "Commands:"
        echo " select <environment>           Select the environment"
        echo " docker_compose <args...>       Interact directly with the docker-compose for the selected environment"
        echo " logs                           Show docker-compose logs"
        echo
        echo " start                          Starts piston"
        echo " stop                           Stops piston"
        echo " restart                        Restarts piston"
        echo " bash                           Opens a bash shell for the piston_api container"
        echo
        echo " update                         Fetches and applies latest updates"
        echo
        echo " <args..>                       Passthrough to piston cli tool"
        echo
        echo "Development Commands:"

        if [ "$PISTON_ENV" == dev ]; then

        echo " clean-pkgs                              Clean any package build artifacts on disk"
        echo " clean-repo                              Remove all packages from local repo"
        echo " list-pkgs                               Lists all packages that can be built"
        echo " build-pkg <package> <version> [builder] Build a package [with desired builder image]"
        echo " rebuild                                 Build and restart the docker container"
        echo " lint                                    Lint the codebase using prettier"

        else

        echo " Switch to developement environment for more info"
        echo " > piston select dev"

        fi
        ;;


    select) echo "$2" > .piston_env ;;
    docker_compose) shift; docker_compose "$@";;
    logs) docker_compose logs -f ;;

    restart) docker_compose restart ;;
    start)
        docker_compose up -d
        ;;
    stop) docker_compose down ;;
    bash) docker_compose exec api /bin/bash ;;

    rebuild)
        docker_compose build && docker_compose up -d
        ;;

    update)
        git pull
        cd cli && npm i > /dev/null && cd -
        docker_compose pull
        docker_compose up -d
        ;;

    clean-pkgs) git clean -fqXd packages ;;
    clean-repo) git clean -fqXd repo ;;

    list-pkgs) find packages -maxdepth 2 | awk -F/ '$2 && $3{ print $2 "-" $3 }' | column ;;

    build-pkg)
        PKGSLUG="$2-$3"
        BUILDER="${4:-piston-repo-builder}"
        echo "Building $PKGSLUG"
        echo "Ensuring latest builder image"
        docker build repo -t "$BUILDER"
        docker run --rm -v "$PWD:/piston" "$BUILDER" --no-server "$PKGSLUG"
        ;;

    lint)
        npm install
        npx prettier --ignore-unknown --write .
        ;;
    *)
        [ -d ./cli/node_modules ] || npm i > /dev/null
        cd "$EXECUTION_PATH"
        node "${PISTON_PATH}/cli/index.js" "$@"
        ;;
esac
